package com.grad.webstudy.controller;

import com.github.pagehelper.PageInfo;
import com.grad.webstudy.annota.ApiAuthorization;
import com.grad.webstudy.annota.LoginAuthorization;
import com.grad.webstudy.constants.AuthConstants;
import com.grad.webstudy.model.Role;
import com.grad.webstudy.service.RoleService;
import com.grad.webstudy.service.RoleUserService;
import com.grad.webstudy.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/2/2 14:32.
 */

//todo 权限验证
@RestController
@RequestMapping("/role")
public class RoleController {


    @Autowired
    private RoleService roleService;


    @Autowired
    private RoleUserService roleUserService;


    @PostMapping("/add")
    @ApiAuthorization(auth = AuthConstants.ROLE_ADD)
    public boolean addRole(String roleName, String roleDesc, String authIds) {
        return roleService.addRole(roleName, roleDesc, authIds);
    }

    @DeleteMapping("/delete")
    @ApiAuthorization(auth = AuthConstants.ROLE_DELETE)
    public boolean deleteRoles(String ids) {
        return roleService.deleteRoles(ids);
    }

    @PutMapping("/update")
    @ApiAuthorization(auth = AuthConstants.ROLE_UPDATE)
    public boolean updateRole(@RequestParam(required = true) Integer id, String roleName, String roleDesc) {
        return roleService.updateRole(id, roleName,roleDesc);
    }

    @GetMapping("/queryRolesByPage")
    @ApiAuthorization(auth = AuthConstants.ROLE_QUERY)
    public PageInfo queryRolesByPage(@RequestParam(defaultValue = "1") int currPage,
                                     @RequestParam(defaultValue = "5") int pageSize,
                                     String keyword) {
        return roleService.queryRolesByPage(currPage, pageSize,keyword.trim());
    }

    @GetMapping("/queryAll")
    @ApiAuthorization(auth = AuthConstants.ROLE_ADD)
    public List<Role> queryAll() {
        return roleService.queryAllRoles();
    }


    /**
     * 更改角色权限
     *
     * @param roleId
     * @param authIds
     * @return
     */
    @PutMapping("/changeRoleAuth")
    @ApiAuthorization(auth = AuthConstants.ROLE_CHANGE_AUTH)
    public boolean changeRoleAuth(@RequestParam(required = true) int roleId, String authIds) {
        return roleService.changeRoleAuth(roleId, authIds);
    }

    /**
     * 查询角色权限
     *
     * @param roleId
     */
    @GetMapping("/queryRoleAuth")
    @ApiAuthorization(auth = AuthConstants.ROLE_QUERY)
    public RoleVo queryRoleAuth(int roleId) {

        return roleService.queryRoleAuth(roleId);
    }


    @GetMapping("/queryUserRoleIds")
    @LoginAuthorization
    public List<Integer> queryUserRoleIds(Integer userId) {
        return roleUserService.queryRoleIdsByUserId(userId);
    }
}
